<template>
  <div class="home">
    <h1>服务端渲染</h1>
    <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item in banners" :key="item.id">
        <img :src="item.address" alt="">
      </el-carousel-item>
    </el-carousel>

    <!-- <p>知识列表</p>
    <ul>
      <li v-for="item in items" :key="item.id">{{ item.name }}</li>
    </ul> -->

    <p>教材列表</p>
    <ul>
      <li v-for="(item, index) in documents" :key="index">{{ item.documentName }}</li>
    </ul>

    <h1>客户端渲染</h1>
    <el-carousel :interval="4000" type="card" height="200px">
      <el-carousel-item v-for="item in bannerClient" :key="item.id">
        <img :src="item.address" alt="">
      </el-carousel-item>
    </el-carousel>

    <p>教材列表</p>
    <ul>
      <li v-for="(item, index) in documentClient" :key="index">{{ item.documentName }}</li>
    </ul>

  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  asyncData ({ store, route }) {
    // 触发 action 后，会返回 Promise, 多个dispatch 使用 Promise.all 处理
    return Promise.all([
      store.dispatch('fetchItems'),
      store.dispatch('fetchBanners'),
      store.dispatch('fetchDocuments')
    ])
  },
  data () {
    return {
      bannerClient: [],
      documentClient: []
    }
  },
  computed: {
    ...mapState({
      items: state => state.common.items,
      banners: state => state.common.banners,
      documents: state => state.common.documents
    })
  },
  mounted () {
    this.$http.get('/v1/web/prefecture/banners_front').then(resp => {
      this.bannerClient = resp.data.data
    }).catch(err => {
      console.log(err.response)
    })

    this.$http.post('/v1/web/prefecture/documents/download_records', {
      beginDate: '2019-07-31',
      endDate: '2019-08-30',
      pageNumber: 1,
      pageSize: 10,
      totalPages: 0
    }).then(resp => {
      this.documentClient = resp.data.data.content
    }).catch(err => {
      console.log(err.response)
    })
  }
}
</script>

<style>
  .el-carousel__item img {
    width: 100%;
    height: 100%;
  }

  .el-carousel__item:nth-child(2n) {
    background-color: #99a9bf;
  }

  .el-carousel__item:nth-child(2n+1) {
    background-color: #d3dce6;
  }
</style>
